package com.chy.blog_server.modular.web.entity;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chy.blog_server.core.util.UserUtil;
import com.chy.blog_server.modular.sharCore.BaseEntity;
import com.chy.blog_server.modular.system.entity.SysUser;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * 描述：书名表 实体类
 * 作者：chenhuiyan
 * 时间：2020/04/10
 */
@Getter
@Setter
@TableName(value = "bo_book")
public class BoBook extends BaseEntity {

    /**
     * 书名
     */
    @TableField(value = "book_name")
    private String bookName;

    /**
     * 简介
     */
    private String content;

    /**
     * 作者
     */
    private String author;

    /**
     * 书类型
     */
    private String type;

    /**
     * 添加数据时调用该方法生成
     * @param request
     */
    public void preInsert(HttpServletRequest request){
        SysUser user = UserUtil.getUserFromSession(request);
        if(ObjectUtil.isNotEmpty(user)){
            this.setCreateBy(user.getId());
            this.setUpdateBy(user.getId());
        }
        this.setId(IdUtil.simpleUUID());
        this.setCreateDate(new Date());
        this.setUpdateDate(new Date());
    }

    /**
     * 更新数据时调用该方法生成
     * @param request
     */
    public void preUpdate(HttpServletRequest request){
        SysUser user = UserUtil.getUserFromSession(request);
        if(ObjectUtil.isNotEmpty(user)){
            this.setUpdateBy(user.getId());
        }
        this.setUpdateDate(new Date());
    }

}
